#include "hotfileparser.h"
#include <QRegExp>
#include <QDebug>
#include <commonutils.h>
#include <QStringList>
#include "captchasolver.h"
#include <QTimer>
#include "links/link.h"





const QString HotFileParser::formName   = "name=f" ;
const QString HotFileParser::site       = "hotfile.com";
const QString HotFileParser::recaptcha  = "api.recaptcha.net";
const QString HotFileParser::pobierz    = "Kliknij tutaj aby pobra";
const QString HotFileParser::usuniety   = "naruszeniem praw autorskich";

QString HotFileParser::type(){
    return site;
}

HotFileParser::HotFileParser()
{
    pendingHash=-1;
    connect(&imgLocator, SIGNAL(imageLocated(QString, QString)), this, SLOT(imgLocated(QString, QString)));
    connect(CaptchaSolver::getInstance(), SIGNAL(solved(int,QString)), this, SLOT(captchaCompetition(int, QString)));
}

int HotFileParser::checkPhase(QString page){
    if(page.contains(formName)){
        return SiteParser::GET_FORM;
    }else if(page.contains(recaptcha)){
        return SiteParser::GET_CAPTCHA;
    }else if(page.contains(pobierz)){
        return SiteParser::GET_FILE;
    }else if(page.contains(usuniety)){
        return SiteParser::FILE_DELETED;
    }
    return -1;
};

void HotFileParser::solveCaptcha(QString page){
    QString form;
    form=CommonUtils::getTagContaining(page,"form", recaptcha);
    if(!form.isEmpty()){
        path=*(CommonUtils::getFormPath(form));
        //qDebug()<< __FILE__<<":"<<__LINE__<<"\t"<< *path;
        post=*(CommonUtils::getFormPost(form));
        imgLocator.getImgAddress(form);
    }
}

void HotFileParser::imgLocated(QString address, QString challange){
    qDebug()<< __FILE__<<":"<<__LINE__<<"\t"<< path;
    pendingHash=CaptchaSolver::getInstance()->solve(address);
    CaptchaSolver::getInstance()->getCaptchaDesc(pendingHash)->setChallange(challange);
};

int checkLongDelay(QString page){
    QRegExp reg("starthtimer\\(\\)\\{([^\\}]*)");
    reg.indexIn(page);
    QString waitFun(reg.cap(1));
    QRegExp reg2("\\+(\\d+)");
    reg2.indexIn(waitFun);
    if(reg2.captureCount()==0){
        qDebug()<<"AAAAAA"<<endl<<"Zmienil sie format strony?????"<<endl<<waitFun;
        return -1;
    }
    return reg2.cap(1).toInt()/1000;
}

void HotFileParser::getForm(QString page){
    int waitT=checkLongDelay(page);
    if(waitT>0){
        //qDebug()<<"Troszke trzeba poczekac:"<<waitT<<"sekund";
        emit wait(waitT);
        return;
    }
    QString form=CommonUtils::getTagContaining(page,"form", formName);
    if(!form.isEmpty()){
        path=*(CommonUtils::getFormPath(form));
        post=*(CommonUtils::getFormPost(form));

        qDebug()<<"Waiting about 1 minute";
        startTm=QTime::currentTime();
        tm=new QTimer;
        connect(tm, SIGNAL(timeout()), this, SLOT(timeout()));
        tm->start(4000);

        return;

    }
    qDebug()<< __FILE__<<":"<<__LINE__<<"\t"<< "didnt found form"<<page;
}

void HotFileParser::timeout(){
    if(startTm.secsTo( QTime::currentTime())>62){
        qDebug()<< __FILE__<<":"<<__LINE__<<"\t"<< "62 sec timeout";
        emit goForm(QUrl(path), QString(post));
        delete tm;
    }else{
        //qDebug()<<"delayed: "<<startTm.secsTo( QTime::currentTime());
    }
}

void HotFileParser::getFile(QString page){
    //get form part of page
    QString form=CommonUtils::getTagContaining(page,"table", pobierz);

    if(!form.isEmpty()){
        QRegExp reg("href=\"([^\"]*)");
        reg.indexIn(form);
        //qDebug()<<"Found file path: "<<reg.cap(1);
        emit goForm(QUrl(reg.cap(1)),"");
    }
}

void HotFileParser::parsePage(QString page){
    switch(checkPhase(page)){
    case SiteParser::GET_FORM:
       getForm(page);
        break;
    case SiteParser::GET_CAPTCHA:
       solveCaptcha(page);
        break;
    case SiteParser::GET_FILE:
       getFile(page);
        break;
    case SiteParser::FILE_DELETED:
       emit error(Link::ERR_DELETED);
        break;
    default:
        qDebug()<<"Nierozpoznana strona:";
        qDebug()<<page;
    };
}

void HotFileParser::captchaCompetition(int hash, QString caption){
    qDebug()<< __FILE__<<":"<<__LINE__<<"Looking for captcha"<<QString().number(pendingHash)<<"on"<<lPath;
    if(pendingHash==hash){
        //qDebug()<< __FILE__<<":"<<__LINE__;
        CaptchaDesc* desc=CaptchaSolver::getInstance()->getCaptchaDesc(hash);
        if(desc==NULL){
            qDebug()<< __FILE__<<":"<<__LINE__<<"some strange captcha problem";
            return;
        }
        desc->setCaption(caption);
        //qDebug()<<__FILE__<<__LINE__<<"Testing captioon"<<path;
        qDebug()<<imgLocator.createPost(desc);

        emit goForm(QUrl(path), imgLocator.createPost(desc));
        CaptchaSolver::getInstance()->rmCaptchaDesc(hash);

        pendingHash=-1;
    }
}
